/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-05-23 19:54:12
 * @LastEditTime: 2024-08-24 09:19:47
 * @FilePath: /nest_study/server/src/app.module.ts
 */
import { Global, Logger, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import configuration from './config/index';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { UserModule } from './modules/system/user/user.module';
import { AccessModule } from './modules/system/access/access.module';
import { HttpModule } from '@nestjs/axios';
import { AxiosModule } from './modules/axios/axios.module';
import { RedisModule } from './modules/redis/redis.module';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { JwtAuthGuard } from './common/guards/auth.guard';
import { AuthModule } from './modules/system/auth/auth.module';
import { RoleModule } from './modules/system/role/role.module';
import { MenuModule } from './modules/system/menu/menu.module';
import { PostModule } from './modules/system/post/post.module';
import { DeptModule } from './modules/system/dept/dept.module';
import { RedisClientOptions } from '@liaoliaots/nestjs-redis';
import { DictModule } from './modules/system/dict/dict.module';
import { OnlineModule } from './modules/monitor/online/online.module';
import { LoginLogModule } from './modules/monitor/login-log/login-log.module';
import { CacheDataModule } from './modules/cache/cache.module';
import winston from 'winston';
import { format } from 'winston';
import { utilities, WinstonModule } from 'nest-winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import { LoggingInterceptor } from './common/decorators/logger-intercaptor';

@Global()
@Module({
  imports: [
    //配置模块
    ConfigModule.forRoot({
      cache: true,
      load: [configuration],
      isGlobal: true
    }),
    //数据库配置
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          type: 'mysql',
          entities: [`${__dirname}/**/*.entity{.ts,.js}`],
          autoLoadEntities: true,
          keepConnectionAlive: true,
          timezone: '+08:00',
          ...config.get('db.mysql')
        } as TypeOrmModuleOptions
      }
    }),
    //redis配置
    RedisModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          closeClient: true,
          readyLog: true,
          errorLog: true,
          config: config.get<RedisClientOptions>('redis'),
        }
      }
    }, true),
    //配置日志
    WinstonModule.forRoot({
      transports: [
        new DailyRotateFile({
          level: 'info',
          filename: "logs/info-%DATE%.log",
          datePattern: "YYYY-MM-DD.HH",
          zippedArchive: true,
          // 文件大小
          maxSize: '20m',
          // 最多14 天
          maxFiles: '14d',
          format: winston.format.combine(
            format.timestamp({
              format: 'YYYY-MM-DD HH:mm:ss',
            }),
            winston.format.printf((info) => {
              return `时间:${info.timestamp},日志类型:${info.level}${info?.context ? `,运行背景: ${info.context}` : '' },日志信息: ${info.message}`
            })
          ),
        }),
      ]
     }),
    HttpModule,
    AccessModule,
    UserModule,
    RoleModule,
    PostModule,
    MenuModule,
    DeptModule,
    DictModule,
    OnlineModule,
    LoginLogModule,
    CacheDataModule,
    AuthModule,
    AxiosModule
  ],
  controllers: [],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard
    }
  ],
})

export class AppModule { }

